package com.pioneer.gui.invoice.util;

import java.util.Vector;
import java.util.StringTokenizer;

/**
 * This class contains a utility function for making strings safe for sqlserver7.
 * it replaces " with "" and ' with ''
 */
public class SqlHelper
{
  public static void main(String[] args)
  {
    String s="\'asdf\"sdf\'asdf\"\'sdf";
    String n =SqlHelper.sqlSafe(s);
    System.out.println(n);
  }

  public static String sqlSafe(String sql)
        {
          int howMany = sql.length();
          StringBuffer sb= new StringBuffer();
          char singleQuote = '\'';
          char doubleQuote='\"';

          for(int i=0;i<howMany;i++)
          {
            char c = sql.charAt(i);
            if(c==singleQuote){
              sb.append(singleQuote);
              sb.append(singleQuote);
            }
            else if(c==doubleQuote){
              sb.append(doubleQuote);
              sb.append(doubleQuote);
            }
            else{
              sb.append(c);
            }
          }
          return sb.toString();
        }
  
  
	public static java.sql.Date toSqlDate(java.util.Date pDate)
	{
		if (pDate == null) {
			return null;
		} else {
			return new java.sql.Date(pDate.getTime());
		}
	}
	
	
	public static String toSqlTimestampString(java.util.Date pDate)
	{
		if (pDate == null) {
			return "null";
		} else {
			return new java.sql.Timestamp(pDate.getTime()).toString();
		}
	}
	
	
}
